<?php
namespace app\jincheng\controller;
use think\Db;
use think\Controller;
use think\Log;
class User extends Controller{

	public function roomlist(){
		$user_id=request()->post("user_id");
		$roomlist=Db::table("roominfo_user")->where("user_id",$user_id)->select();
		if(empty($roomlist)){
			return json(["errCode"=>"1","errormsg"=>"您还没有绑定房屋","roomlist"=>$roomlist]);
		}
		foreach ($roomlist as $key => $value) {
			$roominfo=Db::table("roominfo")->where("id",$value["room_id"])->find();
			$roomlist[$key]["summoney"]=Db::table("bill")->where("out_room_id",$roominfo["out_room_id"])->where("status","WAIT_PAYMENT")->sum("bill_entry_amount");
		}
		return json(["errCode"=>"0","roomlist"=>$roomlist]);
	}

	public function getcommunity(){
		$user_id=request()->post("user_id");
		Log::write($user_id,"user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1"]);
		}
		
		$res=Db::table("applet_community")->where("user_id",$user_id)->find();
		if(!empty($res)){
			return json(["errCode"=>"0","adcode"=>$res["community_id"],"city"=>$res["community_name"]]);
		}else{
			return json(["errCode"=>"1"]);
		}
		
	}
	public function communityupdate(){
		$user_id=request()->post("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1"]);
		}
		$data["community_id"]=request()->post("adcode");
		$data["community_name"]=request()->post("city");
		$res=Db::table("applet_community")->where("user_id",$user_id)->find();
		if(!empty($res)){
			Db::table("applet_community")->where("user_id",$user_id)->update($data);
		}else{
			$data["user_id"]=$user_id;
			Db::table("applet_community")->insert($data);
		}
		return json(["errCode"=>"0"]);
	}
	public function billlistpaid(){
		$user_id=request()->post("user_id");
		//Log::write($user_id,"user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1","errormsg"=>"用户信息错误"]);
		}
		$billlist=Db::table("bill")
				->where("user_id",$user_id)
				->where("status",'TRADE_SUCCESS')
				->select();
		if(empty($billlist)){
			return json(["errCode"=>"1","errormsg"=>"未查到账单"]);
		}
		//Log::write($billlist,"123344");
		return json(["errCode"=>"0","billlist"=>$billlist,"errormsg"=>"未查到账单"]);
	}
}